package net.adst.ui.mainframe.tabs.nanemTap;

import net.adst.resources.Constants;
import net.adst.resources.VariableNames;
import net.adst.util.DataManager;
import net.adst.util.GBC;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import java.awt.*;

/**
 * Created with IntelliJ IDEA.
 * User: alekseipot
 * Date: 02.11.12
 * Time: 6:54
 * To change this template use File | Settings | File Templates.
 */
public class SumNaemTablePanel extends JPanel {

    private JTable sumTable;

    public SumNaemTablePanel() {
        setLayout(new GridBagLayout());
        addTable();
    }

    void addTable() {
        DefaultTableModel model = new DefaultTableModel( //NotEditableDefaultTableModel
                new Object[][]{
                },
                Constants.NAEM_SUM_НEADER
        ) {
            @Override
            public boolean isCellEditable(final int row, final int col) {
                return false;
            }
        };
        sumTable = new JTable(model);
        sumTable.setRowHeight(Constants.SUM_TABLE_ROW_HEIGHT);
        sumTable.getTableHeader().setReorderingAllowed(false);
        final JLabel sumTableLabel = new JLabel("Итоговая таблица:");
        add(sumTableLabel, new GBC(0, 0).setInsets(1, 5, 0, 0).setAnchor(GBC.WEST));
        DataManager.setObject(VariableNames.UI_NAEM_SUM_TABLE, sumTable);
        DataManager.setObject(VariableNames.UI_NAEM_SUM_MODEL, model);
        sumTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                if (e.getValueIsAdjusting())
                    return;
                if (!((ListSelectionModel) e.getSource()).isSelectionEmpty()) {
                    final JTable detTable = (JTable) DataManager.getObject(VariableNames.UI_NAEM_DETAILED_TABLE);
                    detTable.getSelectionModel().clearSelection();
                }
            }
        }
        );
        JScrollPane scrollPane = new JScrollPane(sumTable);
        scrollPane.setMinimumSize(new

                Dimension(150, 50)

        );

        add(scrollPane, new GBC(0, 1).setFill(GBC.BOTH).setWeight(1, 1).setInsets(5).setAnchor(GBC.SOUTHEAST));
    }
}
